import { Component, Input, OnInit, OnDestroy } from '@angular/core';

import { ProductHistoryService } from './product.history.service';

import { Util } from '../../../../utils/util';


@Component({
  selector: 'product-history',
  templateUrl: './product.history.html',
  providers: [ProductHistoryService]
})

export class ProductHistoryCompoenet {

  @Input() date: any;
  timeType: string = 'day';

  p: any = 1;
  p2: any = 1;
  productInfo: any;
  constructor(private service: ProductHistoryService, private util: Util) {

    this.productInfo = this.util.sessionStore().get('productDetail');

  }

  vm = {
    "loading": true,
    "data": [],
    "column": {
      fileName: '',
      dim: ['date', 'numberId', 'title', 'loc', 'propertyValue', 'price', 'monthPrice', 'monthSellAmount', 'monthSellMoney', 'sellAmount', 'sellMoney', 'brandName', 'uid', 'shopType'],
      dimText: ['日期', '宝贝id', '标题', '地址', '属性', '价格', '近30天成交均价', '近30天销售量', '近30天销售额', '日销售量', '日销售额', '品牌名', '店铺id', '店铺类型']
    },
    "type": 'line-mm'
  };

  columns = ['日期', '日销售量', '日销售额', '价格', '近30天销售量', '近30天销售额', '近30天成交均价'];

  eventOff: any;
  ngOnInit() {

  }

  // change

  ngOnChanges(changes) {

    console.log(changes);

    if (changes.date.currentValue && changes.date.currentValue.timeType == 'day') {

      console.log(this.date);
      this.vm.loading = true;
      this.getHttp(this.productInfo, this.date.date);
    }


  }

  ngOnDestroy() {
    if (this.offEvenet) {
      this.offEvenet.unsubscribe();

    }
    if (this.eventOff) {
      this.eventOff.unsubscribe();

    }
  }

  // 导出
  export() {
    console.log("export");

    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim, this.vm.column.dimText);


    this.util.export(data, this.vm.column.fileName);
  }


  sellResulet: any = {};
  offEvenet: any;
  getHttp(productInfo, date) {

    if (this.vm.loading && this.offEvenet) {
      this.offEvenet.unsubscribe();
    }

    let json = {
      numberId: productInfo.numberId,
      categoryId: productInfo.category || productInfo.categoryId
    };

    Object.assign(json, date);

    this.sellResulet = {
      'sellMoney': 0,
      'sellAmount': 0
    };

    this.vm.column.fileName = date.begin.slice(0, 10) + '~' + date.end.slice(0, 10) + this.productInfo.title;

    var res = {
      "code": 0, "msg": "成功", "data": [
         { "date": "2017-11-10 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 5, "sellMoney": 2490.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-11 00:00:00", "numberId": "37159234393", "title": "【正品联保】Beats URBEATS 2.0重低音降噪入耳式耳机魔音B耳塞式", "picUrl": null, "loc": "浙江杭州", "propertyValue": "是否线控:是,品牌:Beats,耳机插头类型:直插型,套餐类型:官方标配,频响范围:20-20000Hz,耳机售后服务:全国联保,型号:URBEATS,耳机输出音源:随身视听,缆线长度:1.2M,阻抗:32Ω,耳机类型:有线,耳机类别:HIFI耳机,生产企业:美国苹果公司,适用音乐类型:摇滚金属重低音类型,插头直径:3.5mm,有无麦克风:带麦,兼容平台:ANDROID iOS,佩戴方式:入耳式,颜色分类:哑光黑 土豪银 玫瑰金色 土豪金 天空灰 紫色 深灰,保修期:12个月,灵敏度:108dB/mW", "brandName": "beats", "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 2518, "sellMoney": 1253964.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-12 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 0, "sellMoney": 0.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-13 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 3, "sellMoney": 1494.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-14 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 1, "sellMoney": 498.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-15 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 4, "sellMoney": 1992.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-16 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 2, "sellMoney": 996.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-17 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 0, "sellMoney": 0.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-18 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 20, "sellMoney": 9960.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-19 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 39, "sellMoney": 19422.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-20 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 46, "sellMoney": 22908.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-21 00:00:00", "numberId": "37159234393", "title": "【正品联保】Beats URBEATS 2.0重低音降噪入耳式耳机魔音B耳塞式", "picUrl": null, "loc": "浙江杭州", "propertyValue": "是否线控:是,品牌:Beats,耳机插头类型:直插型,套餐类型:官方标配,频响范围:20-20000Hz,耳机售后服务:全国联保,型号:URBEATS,耳机输出音源:随身视听,缆线长度:1.2M,阻抗:32Ω,耳机类型:有线,耳机类别:HIFI耳机,生产企业:美国苹果公司,适用音乐类型:摇滚金属重低音类型,插头直径:3.5mm,有无麦克风:带麦,兼容平台:ANDROID iOS,佩戴方式:入耳式,颜色分类:哑光黑 土豪银 玫瑰金色 土豪金 天空灰 紫色 深灰,保修期:12个月,灵敏度:108dB/mW", "brandName": "beats", "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 28, "sellMoney": 13944.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-22 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 42, "sellMoney": 20916.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-23 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 21, "sellMoney": 10458.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-24 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 28, "sellMoney": 13944.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-25 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 41, "sellMoney": 20418.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-26 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 52, "sellMoney": 25896.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-27 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 46, "sellMoney": 22908.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-28 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 46, "sellMoney": 22908.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-29 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 39, "sellMoney": 19422.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-11-30 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 47, "sellMoney": 23406.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-12-01 00:00:00", "numberId": "37159234393", "title": "【正品联保】Beats URBEATS 2.0重低音降噪入耳式耳机魔音B耳塞式", "picUrl": null, "loc": "浙江杭州", "propertyValue": "是否线控:是,品牌:Beats,耳机插头类型:直插型,套餐类型:官方标配,频响范围:20-20000Hz,耳机售后服务:全国联保,型号:URBEATS,耳机输出音源:随身视听,缆线长度:1.2M,阻抗:32Ω,耳机类型:有线,耳机类别:HIFI耳机,生产企业:美国苹果公司,适用音乐类型:摇滚金属重低音类型,插头直径:3.5mm,有无麦克风:带麦,兼容平台:ANDROID iOS,佩戴方式:入耳式,颜色分类:哑光黑 土豪银 玫瑰金色 土豪金 天空灰 紫色 深灰,保修期:12个月,灵敏度:108dB/mW", "brandName": "beats", "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 54, "sellMoney": 26892.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-12-02 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 31, "sellMoney": 15438.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }, { "date": "2017-12-03 00:00:00", "numberId": "37159234393", "title": null, "picUrl": null, "loc": null, "propertyValue": "", "brandName": null, "price": 498.0, "monthPrice": 498.0, "quantity": 0, "monthSellAmount": 2680, "monthSellMoney": 1334640.0, "sellAmount": 31, "sellMoney": 15438.0, "category": "1205", "catName": null, "uid": "1974623778", "shopName": null, "shopType": "商城", "publishTime": null }]
    };

    setTimeout(() => {

      this.vm.data = res.data;

      // 合计
      this.sellResulet = this.util.getSellAllResult(this.sellResulet, res.data);

      this.vm.loading = false;

    }, 100);


  }



}
